package com.example.androidautoproject.service

import android.util.Log
import com.example.androidautoproject.utils.AutoScript
import com.example.androidautoproject.utils.AdParam
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.findFirstParentClickable
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl

class TouTiaoProject: StepImpl() {
    private val TAG = "adProject"
    private var bool = false
    private var currentClassName = ""
    private var packageName = "com.ss.android.article.lite"
    override fun onImpl(collector: StepCollector) {
        collector.next(stepTag = 1){
            AdParam.isStart = true
            AdParam.adLog = mutableListOf()
            AdParam.currentTag = "adProject"
            start_app()
            return@next Step.get(2)
        }.next(stepTag = 2){
            start_activity()
            return@next Step.get(3)
        }.next(stepTag = 3){
            start_ad()
            return@next Step.none
        }
    }

    suspend fun start_app(){
        Log.e(TAG, "1.开始${TAG}任务")
        AssistsCore.launchApp(packageName)
        Thread.sleep(15000 + AutoScript.ran_time)
    }

    suspend fun start_activity(){
        Log.e(TAG, "2.开始点击赚钱任务")
        AutoScript.ran_click(540f, 2280f)
        Thread.sleep(10000 + AutoScript.ran_time)
        if(AutoScript.click_element_by_text("今日签到领")){
            AutoScript.click_element_by_text("直接领取")
            Thread.sleep(2000 + AutoScript.ran_time)
        }
        AutoScript.closeWinBySizeOnly(103, 103)
    }

    suspend fun start_ad(){
        Log.e(TAG, "3.开始点击看广告赚钱")
        while(true){
            if(AdParam.isStart) {
                if(AdParam.packageName == packageName){
                    check_ele()
                    Thread.sleep(1000 + AutoScript.ran_time)
                    check_ad()
                    Thread.sleep(1000 + AutoScript.ran_time)
                } else {
                    if(AdParam.packageName == "com.huawei.android.internal.app" && AdParam.className == "android.app.Dialog"){
                        Thread.sleep(2000+ AutoScript.ran_time)
                        AutoScript.ran_click(300f, 2000f)
                        Thread.sleep(5000+ AutoScript.ran_time)
                        AssistsCore.back()
                        Thread.sleep(5000+ AutoScript.ran_time)
                        AutoScript.close_app()
                        Thread.sleep(2000+ AutoScript.ran_time)
                        AutoScript.ran_click(600f, 1500f)
                    }
                    Thread.sleep(2000+ AutoScript.ran_time)
                    AssistsCore.launchApp(packageName)
                    Thread.sleep(10000+ AutoScript.ran_time)
                    AdParam.className = currentClassName
                }
            }else{
                Log.e(TAG, "3.结束任务")
                break
            }
        }
    }

    suspend fun check_ele(){
        Thread.sleep(3000 + AutoScript.ran_time)
        AutoScript.click_element_by_textAndPosition("看广告赚金币", 950, 820)
        Thread.sleep(3000 + AutoScript.ran_time)
        AutoScript.click_element_by_textAndPosition("看视频再得", 800, 1400)
        Thread.sleep(3000 + AutoScript.ran_time)
        AutoScript.click_element_by_textAndPosition("看视频领", 800, 1400)
        Thread.sleep(3000 + AutoScript.ran_time)
    }
    suspend fun check_ad(){
        if(AdParam.className == "com.ss.android.live.host.livehostimpl.OpenLivePlayerActivity" || AdParam.className.indexOf("com.ss.android.live.host") >= 0){
            currentClassName = "com.ss.android.live.host.livehostimpl.OpenLivePlayerActivity"
            Thread.sleep(5000+ AutoScript.ran_time)
            AssistsCore.back()
        } else if(AdParam.className == "com.bytedance.android.sif.container.SifContainerActivity"){
            currentClassName = "com.bytedance.android.sif.container.SifContainerActivity"
            Thread.sleep(5000+ AutoScript.ran_time)
            AssistsCore.back()
        } else{
            if(AutoScript.find_element_by_text("领取成功")){
                AutoScript.closeWinBySizeOnly(24, 24)
                Thread.sleep(1000+ AutoScript.ran_time)
            }
            AutoScript.click_elementById("com.ss.android.article.lite:id/z7")
            Thread.sleep(1000+ AutoScript.ran_time)
        }
    }
}

